package nl.haemers.data.reflection 
{
	import flash.utils.getDefinitionByName;
	import nl.haemers.data.constants.DataTypes;
	import nl.haemers.data.serializers.ObjectSerializer;
	import nl.haemers.data.utils.ObjectUtils;
	/**
	 * ...
	 * @author Jonathan Haemers
	 */
	public class Parameter implements IParameter 
	{
		private var _name							:String;
		private var _index							:int;
		private var _dataType						:String;
		private var _optional						:Boolean;
		private var _objType						:Type;
		
		private var _xmlDataType					:String;
		
		public function Parameter(description:XML, name:String = null) 
		{
			init(description, name);
		}
		
		/** Public methods **/
		public function toString() : String
		{
			return _name;
		}
		/** Getters & Setters **/
		public function get index() : int 
		{
			return _index;
		}
		
		public function get name() : String
		{
			return _name;
		}
		
		public function get dataType() : String 
		{
			return _dataType;
		}
		
		public function get optional() : Boolean 
		{
			return _optional;
		}
		
		public function get objectType() : Type
		{
			if (_objType == null && !ObjectUtils.isDataType(this.dataType))
			{
				//this._objType = createType(this);
				this._objType = _xmlDataType ? createType(_xmlDataType) : createType(this.dataType);
			}
			
			return _objType;
		}
		
		/** Static methods **/
		/*<parameter index="1" type="String" optional="false"/>*/
		private function init(xml:XML, name:String = null) : void//Boolean
		{
			//var parameter:Parameter = new Parameter();
			this._index = xml.@index;
			this._dataType = xml.@type;
			this._optional = xml.@optional === "true";
			
			this._name = (name) ? name : String("param_" + this.index);
			
			if (ObjectUtils.isVector(this.dataType))
			{
				this._xmlDataType = this._dataType;
				this._dataType = DataTypes.VECTOR;
			}
			//return true;
		}
		/** Protected methods **/
		
		/** Private methods **/
		//private function createType(parameter:IParameter) : Type
		//{
			//var classRef:Class = getDefinitionByName(parameter.dataType) as Class;
		
		private function createType(type:String) : Type
		{
			var classRef:Class = getDefinitionByName(type) as Class;
			return ObjectSerializer.getType(classRef);
		}
	}

}